/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package btnhom;

import java.io.*;
import java.sql.Connection;
import oracle.jdbc.OracleResultSet;
import oracle.jdbc.OraclePreparedStatement;
import conn.JavaConnectDb;
import javax.swing.JOptionPane;
/**
 *
 * @author lhqbao
 */

public class Quanhuyen {
    private Integer ma_qh;
    private String ten_qh;
    
    Connection conn;
    OraclePreparedStatement pst;
    OracleResultSet rs;

    public Integer getMa_qh() {
        return ma_qh;
    }

    public void setMa_qh(Integer ma_qh) {
        this.ma_qh = ma_qh;
    }

    public String getTen_qh() {
        return ten_qh;
    }

    public void setTen_qh(String ten_qh) {
        this.ten_qh = ten_qh;
    }
    
    public OracleResultSet SelectQuanHuyen(){
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "select * from quanhuyen";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            rs = (OracleResultSet) pst.executeQuery();
            
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Khong the ket noi server");
        }
        return rs;
        
    } 
    public OracleResultSet SelectMaqh(String ten_qh){
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "select ma_qh from quanhuyen where ten_qh=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, ten_qh.trim());
            rs = (OracleResultSet) pst.executeQuery();
            
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Khong the ket noi server");
        }
        return rs;
        
    } 
    public void insertQuanhuyen(String maqh, String tenqh)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "insert into quanhuyen(ma_qh, ten_qh) values(?,?)";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, maqh);
            pst.setString(2, tenqh);
            pst.executeQuery();
                    
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Khong the ket noi server khi cap nhat");
        }finally {
        //try { rs.close(); } catch (Exception e) { /* ignored */ }
       // try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
    public void updateQuanhuyen(String maqh, String tenqh)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "update quanhuyen set ten_qh=? where ma_qh=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(2, maqh);
            pst.setString(1, tenqh);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "Cap nhat thanh cong");        
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Cap nhat khong thanh cong");
        } finally {
       // try { rs.close(); } catch (Exception e) { /* ignored */ }
        //try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
    public void deleteQuanhuyen(String maqh)
    {
        conn = JavaConnectDb.ConnectDb();
        try{
            String sql = "delete from quanhuyen where ma_qh=?";
            pst=(OraclePreparedStatement) conn.prepareStatement(sql);
            pst.setString(1, maqh);
            pst.executeQuery();
            JOptionPane.showMessageDialog(null, "Xoa thanh cong");        
         }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Xoa khong thanh cong");
        } finally {
        //try { rs.close(); } catch (Exception e) { /* ignored */ }
        //try { pst.close(); } catch (Exception e) { /* ignored */ }
        try { conn.close(); } catch (Exception e) { /* ignored */ }
        }
    }
}
